package Server;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

/**
 * handles the creation, update, saving, and logging in of users
 * @author David
 *
 */
public class UserHandler {

	/**
	 * create output stream for user list
	 */
	static Writer listOut = null;
	BufferedReader listIn = null;
	String fileName = "userList.txt";
	File file = new File(fileName);


	public UserHandler(){

		if (!file.exists()){
			try {
				file.createNewFile();
				System.out.println("userList created");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			listOut = new BufferedWriter(new FileWriter(file));
			listIn = new BufferedReader(new FileReader("userList.txt"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @param user
	 * @param pass
	 * @param access
	 */
	public static void createUser(String user, String pass, String access){
		try {
			listOut.write(user + "," + pass + "," + access + "\n");
			listOut.flush();
			System.out.println("printed to userList");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @param user
	 * @param Pass
	 * @throws IOException 
	 */
	public String login(String user, String Pass) throws IOException{

		String str = listIn.readLine();
		String[] strarray = str.split(",");
		String name = strarray[0];
		String pass = strarray[1];

		while(!(name.equalsIgnoreCase(user) && pass.equalsIgnoreCase(Pass))){
			if(str == null)
			{
				System.out.println("User does not exist");
				return null;
			}
			str = listIn.readLine();
			strarray = str.split(",");
			name = strarray[0];
			pass = strarray[1];

		}
		return(strarray[2]);
	}

	public void delete(String user) throws IOException{
		File tempFile = new File ("mytempfile.txt");
		BufferedWriter tempout = new BufferedWriter(new FileWriter(tempFile));
		if (!tempFile.exists()){
			try {
				file.createNewFile();
				//System.out.println("userList created");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			tempout = new BufferedWriter(new FileWriter(tempFile));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String str = listIn.readLine();
		String[] strarray = str.split(",");
		String name = strarray[0];
		
		while(str != null){
			//System.out.println(name);
			if(!(name.equalsIgnoreCase(user)))
			{
				//System.out.println(name);
				tempout.write(str);
				tempout.flush();
			}
			str = listIn.readLine();
			if(str != null){
				strarray = str.split(",");
				name = strarray[0];
				//System.out.println(name);
			}
		}
		listIn.close();
		tempout.close();
		file.delete();
		tempFile.renameTo(file);
	}

//	public static void main(String[] args) throws IOException{
//		UserHandler uh = new UserHandler();
//		uh.createUser("justin" , "abc" , "admin");
//		uh.createUser("david" , "dfs" , "dsf");
//		uh.delete("david");	
//		//uh.login("abc", "ads");
//		//uh.login("justin", "abc");
//		//System.out.println(uh.login("Justin", "abc"));
//	}

} 	
